package edu.cuit.zhuyimeng.flight.controller;

import edu.cuit.zhuyimeng.framework.common.result.CommonResult;
import edu.cuit.zhuyimeng.framework.common.result.HttpStatusCodeConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员流程控制器
 */
@RequestMapping("/process")
@RequiredArgsConstructor
@RestController
@Tag(name = "管理员流程控制器")
public class ProcessController {

    private final RuntimeService runtimeService;
    private final RepositoryService repositoryService;

    @DeleteMapping("/")
    @Operation(summary = "删除流程实例",security = @SecurityRequirement(name = "Authorization"))
    public CommonResult<Void> deleteProcessInstance(@RequestParam("id") String id,@RequestParam("reason") String reason) {
        try {
            runtimeService.deleteProcessInstance(id,reason);
        } catch (Exception e) {
            return CommonResult.error(HttpStatusCodeConstants.BAD_REQUEST,e.getMessage());
        }
        return CommonResult.success();
    }

    @PutMapping("/definition")
    @Operation(summary = "部署流程定义",security = @SecurityRequirement(name = "Authorization"))
    public CommonResult<Void> deployProcessDefinition(@RequestParam("name") String name,@RequestParam("filename")String filename) {
        repositoryService.createDeployment()
                .addClasspathResource("processes/" + filename)
                .name(name)
                .deploy();
        return CommonResult.success();
    }

    @DeleteMapping("/definition")
    @Operation(summary = "删除流程定义",security = @SecurityRequirement(name = "Authorization"))
    public CommonResult<Void> deleteProcessDefinition(@RequestParam("id") String id) {
        repositoryService.deleteDeployment(id);
        return CommonResult.success();
    }

}
